package com.tcmj.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tcmj.common.annotation.Log;
import com.tcmj.common.enums.BusinessType;
import com.tcmj.system.domain.BizIncomeplayinfo;
import com.tcmj.system.service.IBizIncomeplayinfoService;
import com.tcmj.common.core.controller.BaseController;
import com.tcmj.common.core.domain.AjaxResult;
import com.tcmj.common.utils.poi.ExcelUtil;
import com.tcmj.common.core.page.TableDataInfo;

/**
 * 收支记录Controller
 * 
 * @author tcmj
 * @date 2020-04-21
 */
@Controller
@RequestMapping("/system/incomeplayinfo")
public class BizIncomeplayinfoController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(BizIncomeplayinfoController.class);
    private String prefix = "system/incomeplayinfo";

    @Autowired
    private IBizIncomeplayinfoService bizIncomeplayinfoService;

    @RequiresPermissions("system:incomeplayinfo:view")
    @GetMapping()
    public String incomeplayinfo(ModelMap mmap)
    {
    	BizIncomeplayinfo bizIncomeplayinfo = new BizIncomeplayinfo();
    	List<BizIncomeplayinfo> list = bizIncomeplayinfoService.selectBizIncomeplayinfoList(bizIncomeplayinfo);
        double incomeStr = 0;
        double playStr = 0;
        double balanceStr = 0;
        for(BizIncomeplayinfo BizIncomeplayinfo:list){
        	if(BizIncomeplayinfo.getPlayType()==0){
        		incomeStr += BizIncomeplayinfo.getProducePrice();
        	}else if(BizIncomeplayinfo.getPlayType()==1){
        		playStr += BizIncomeplayinfo.getProducePrice();
        	}
        }
        balanceStr = incomeStr - playStr;
        mmap.put("incomeStr",incomeStr);
        mmap.put("playStr",playStr);
        mmap.put("balanceStr",balanceStr);
        log.info("========incomeStr=======["+incomeStr+"]");
        log.info("========playStr=======["+playStr+"]");
        log.info("========balanceStr=======["+balanceStr+"]");
        return prefix + "/incomeplayinfo";
    }

    /**
     * 查询收支记录列表
     */
    @RequiresPermissions("system:incomeplayinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizIncomeplayinfo bizIncomeplayinfo,ModelMap mmap)
    {
        startPage();
        List<BizIncomeplayinfo> list = bizIncomeplayinfoService.selectBizIncomeplayinfoList(bizIncomeplayinfo);
        return getDataTable(list);
    }

    /**
     * 导出收支记录列表
     */
    @RequiresPermissions("system:incomeplayinfo:export")
    @Log(title = "收支记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizIncomeplayinfo bizIncomeplayinfo)
    {
        List<BizIncomeplayinfo> list = bizIncomeplayinfoService.selectBizIncomeplayinfoList(bizIncomeplayinfo);
        ExcelUtil<BizIncomeplayinfo> util = new ExcelUtil<BizIncomeplayinfo>(BizIncomeplayinfo.class);
        return util.exportExcel(list, "incomeplayinfo");
    }

    /**
     * 新增收支记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    
    /**
     * 新增保存收支记录
     */
    @Log(title = "统计", businessType = BusinessType.OTHER)
    @PostMapping("/total")
    @ResponseBody
    public AjaxResult total(BizIncomeplayinfo bizIncomeplayinfo)
    {
    	AjaxResult ajax = new AjaxResult();
    	List<BizIncomeplayinfo> list = bizIncomeplayinfoService.selectBizIncomeplayinfoList(bizIncomeplayinfo);
    	double incomeStr = 0;
        double playStr = 0;
        double balanceStr = 0;
        for(BizIncomeplayinfo BizIncomeplayinfo:list){
        	if(BizIncomeplayinfo.getPlayType()==0){
        		incomeStr += BizIncomeplayinfo.getProducePrice();
        	}else if(BizIncomeplayinfo.getPlayType()==1){
        		playStr += BizIncomeplayinfo.getProducePrice();
        	}
        }
        balanceStr = incomeStr - playStr;
        String msg = incomeStr+"#"+playStr+"#"+balanceStr;
        ajax.put("code", 200);
        ajax.put("msg", msg);
        log.info("========ajax msg=======["+msg+"]");
        return ajax;
    }
    
    /**
     * 新增保存收支记录
     */
    @RequiresPermissions("system:incomeplayinfo:add")
    @Log(title = "收支记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizIncomeplayinfo bizIncomeplayinfo)
    {
        return toAjax(bizIncomeplayinfoService.insertBizIncomeplayinfo(bizIncomeplayinfo));
    }

    /**
     * 修改收支记录
     */
    @GetMapping("/edit/{produceId}")
    public String edit(@PathVariable("produceId") Long produceId, ModelMap mmap)
    {
        BizIncomeplayinfo bizIncomeplayinfo = bizIncomeplayinfoService.selectBizIncomeplayinfoById(produceId);
        mmap.put("bizIncomeplayinfo", bizIncomeplayinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存收支记录
     */
    @RequiresPermissions("system:incomeplayinfo:edit")
    @Log(title = "收支记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizIncomeplayinfo bizIncomeplayinfo)
    {
        return toAjax(bizIncomeplayinfoService.updateBizIncomeplayinfo(bizIncomeplayinfo));
    }

    /**
     * 删除收支记录
     */
    @RequiresPermissions("system:incomeplayinfo:remove")
    @Log(title = "收支记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizIncomeplayinfoService.deleteBizIncomeplayinfoByIds(ids));
    }
}
